<?php
/*******************************************************************
 * FlexRemotingServiceConfig.php
 * Copyright (C) 2006-2007 Midnight Coders, LLC
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is WebORB Presentation Server (R) for PHP.
 * 
 * The Initial Developer of the Original Code is Midnight Coders, LLC.
 * All Rights Reserved.
 ********************************************************************/
 

 require_once(WebOrb . "Util/Invocation.php");
 require_once(WebOrb . "Util/ObjectFactories.php"); 
 require_once(WebOrb . "Util/XmlUtil.php"); 
 require_once(WebOrb . "Security/AccessConstraint.php");  
 
class FlexRemotingServiceConfig
{
    const REMOTINGSERVICE_FILE = "remoting-config.xml";
		
    public static function configure($orbConfig) 
    {
        $dom = new DomDocument();
        $dom->load($orbConfig->getFlexConfigPath() . FlexRemotingServiceConfig::REMOTINGSERVICE_FILE);
        
		$root = $dom->documentElement;
		$destinationNodes = $root->getElementsByTagName( "destination" );
		
        foreach($destinationNodes as $destElement)
        {

          $destinationId = $destElement->getAttribute( "id" );
          $props = $destElement->getElementsByTagName( "properties" )->item(0);
          $source = XmlUtil::getElementText($props,"source");
          $scope = XmlUtil::getElementText( $props, "scope" );
          
          $context = array();
          
          if($scope != null && strlen(trim($scope)) > 0)
          {
              $context[ORBConstants::ACTIVATION] = $scope;
          }
          
          $orbConfig->getServiceRegistry()->_addMapping($destinationId, $source, $context );
          
          
          $securityNodes = $destElement->getElementsByTagName( "security" );
          
          if($securityNodes != null && $securityNodes->length > 0 )
          {
            
             $securityElement = $securityNodes->item(0);
             $securityConstraintNode = $securityElement->getElementsByTagName( "security-constraint" )->item(0);
             $rolesNode = $securityConstraintNode->getElementsByTagName( "roles" )->item(0);
             $rolesNodeList = $rolesNode->getElementsByTagName( "role" );
             $constraint = new AccessConstraint($source . "_constraint", "grant" );
             
             for($i = 0; $i < $rolesNodeList->length; $i++)             
                $constraint->addRole($rolesNodeList->item($i)->nodeValue);
            
             $security = $orbConfig->getSecurity();
             $constraintsList = &$security->getConstraintsList();
             $constraintsList[$constraint->getName()] = $constraint;
             
             $constraints = array($constraint->getName());
 
             
             $security->secureResource($source,$constraints,null);
             
          }
        }
    }
}
?>